#include "stopwatch.h"
#include "soc/soc_misc.h"

StopWatch::StopWatch()
{
}

StopWatch::~StopWatch()
{
    Stop();
}

void StopWatch::Start()
{
    if(m_isRunning == true)
        return;

    m_isRunning = true;
    m_startMs = SOC_Misc::CurrentCpuMilliseconds();
    m_startUs = SOC_Misc::CurrentCpuUs();
}

void StopWatch::ReStart()
{
    Stop();
    Start();
}

void StopWatch::Stop()
{
    if(m_isRunning == false)
        return;

    m_endMs = SOC_Misc::CurrentCpuMilliseconds();
    m_endUs = SOC_Misc::CurrentCpuUs();
    m_isRunning = false;
}

uint32_t StopWatch::ElapsedMilliseconds()
{
    if(m_isRunning)
    {
        return SOC_Misc::CurrentCpuMilliseconds() - m_startMs;
    }
    else 
    {
        return m_endMs - m_startMs;
    }
}

uint32_t StopWatch::ElapsedMicroSeconds()
{
    uint32_t us = 0;
    if(m_isRunning)
    {
        us = SOC_Misc::CurrentCpuUs() - m_startUs;
    }
    else 
    {
        us = m_endUs - m_startUs;
    }

    return us;
}
